

global firstyear 1996
global lastyear 2010
global countrysample_iso2 "AT BE DE DK ES FI GB GR IT NL PT SE"

// added later to the dataset 

use $countrypath/gdp_marketprice, clear 

g iso2="."
replace iso2="DE" if country=="Germany (until 1990 former territory of the FRG)"
replace iso2="AT" if country=="Austria"
replace iso2="BE" if country=="Belgium"
replace iso2="DK" if country=="Denmark"
replace iso2="ES" if country=="Spain"
replace iso2="FI" if country=="Finland"
replace iso2="GB" if country=="United Kingdom"
replace iso2="GR" if country=="Greece"
replace iso2="IT" if country=="Italy"
replace iso2="NL" if country=="Netherlands"
replace iso2="PT" if country=="Portugal"
replace iso2="SE" if country=="Sweden"

drop if iso2=="."

sort year trim 
egen t=group(year trim) 
egen c=group(iso2)
xtset c t 
destring gdp, replace
g gr_gdp=gdp/L4.gdp - 1
egen m=mean(gr_gdp), by(iso2) 
egen sd=sd(gr_gdp), by(iso2) 

g crisis=gr_gdp<m - sd 
drop c t
keep crisis gr_gdp iso2 trim year 
destring year, replace
save tmp2, replace

// here starts the construction per se 

use transaction_all_id_conc$firstyear$lastyear.dta, clear 
g trim=1 if month<4
replace trim=2 if month>=4 & month<7
replace trim=3 if month>=7 & month<10
replace trim=4 if month>=10

g insample=0
foreach i in $countrysample_iso2 {
	replace insample=1 if iso2=="`i'"
}
keep if insample==1


collapse (sum) export, by(year trim iso2 id_conc siren importer)
id_group id_sbp "iso2 importer id_conc siren"
egen t=group(year trim) 
xtset id_sbp t 

egen tot=sum(export), by(id_conc iso2 t)

foreach i in export tot{
g `i'0=L4.`i'
replace `i'0=0 if `i'0==. 
}

foreach i in export tot{
g `i'1=F4.`i'
replace `i'1=0 if `i'1==. 
}

g w=export+export0 // (tot+tot0)
g w1=export+export1 // (tot+tot1)

g g_all=w*(export-export0)/(export/2+export0/2)
g g_all1=w1*(export1-export)/(export/2+export1/2)

g entri=g_all*(export0==0)
g exit=g_all1*(export1==0)
g intens=g_all*(export0>0 & export>0) 

g _=(export0>0 & export>0)
egen max=max(_), by(siren iso2 trim year) // 0 if the firm was not exporting to this destination
g entri_dest=g_all*(max==0) 
drop max _

g _=(export1>0 & export>0)
egen max=max(_), by(siren iso2 trim year) // 0 if the firm wont be exporting to this destination
g exit_dest=g_all1*(max==0) 
drop max _


collapse (sum)  entri exit intens export entri_dest exit_dest, by(year trim t id_conc iso2) 
egen pc=group(id_conc iso2) 
drop t
egen t=group(year trim)
xtset pc t
g export0=L4.export 
replace export0=0 if export0==.

g exitc=L4.exit
g exitc_dest=L4.exit_dest

replace exitc=0 if exitc==.
replace exitc_dest=0 if exitc_dest==.

foreach i in entri exitc intens exitc_dest entri_dest{
replace `i' = `i'/(export+export0) 
} 

g g_all=2*(export-export0)/(export+export0)

g test=g_all-(intens + exitc + entri)
sum test, d // awesome 

destring year, replace
merge m:1 iso2 trim year using $countrypath/uncertainty
keep if _m==3
drop _m

merge m:1 iso2 trim year using tmp2
keep if _m==3
drop _m


egen pct=group(pc t)
gen p=group(id_conc)

save decompo_margin_$firstyear$lastyear, replace

erase tmp2.dta 